package com.xiaoke.service.impl;

import com.xiaoke.entity.Address;
import com.xiaoke.mapper.AddressMapper;
import com.xiaoke.service.AddressService;
import com.xiaoke.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private  SnowflakeIdWorker snowflakeIdWorker;
    @Override
    @Transactional(readOnly = false)
    public void delete(String id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void insert(@RequestBody Address address) {
        address.setId(String.valueOf(snowflakeIdWorker.nextId()));
        addressMapper.insert(address);
    }

    @Override
    public void update(@RequestBody Address address) {
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public Map<String, Object> findById(String id) {
        return addressMapper.findById(id);
    }

    @Override
    public List<Map<String, Object>> findByUser(String userId) {
        return addressMapper.findByUser(userId);
    }

    @Override
    public Map<String, Object> getDefaultAddress(String userId) {
        return addressMapper.getDefaultAddress(userId);
    }
}
